import { Effect } from "./effect/effect";
import { EffectType } from "../consts/effect_type";
import { UnitType } from "../consts/unit_type";

export class Unit{
    unitId:string;
    attackOverTime:number;//攻击时间记录
    effects:Effect[]=[];
    attack:number;//攻击力
    private curHp:number;
    maxHp:number;
    attackCD:number = 1000;//攻击CD
    isDied:Boolean=false;
    unitType:UnitType;
    pushId:string;

    setCurHp(value){
      if(value<=0){
        this.isDied=true;
      }
      this.curHp=value;
    }
 
    getCurHp(){
      return this.curHp;
    }
     
    run() { // 运行角色身上的效果
        if(this.effects.length===0){
          return;
        }
        for(let i=0;i<this.effects.length;i++) {
            let e:Effect = this.effects[i];
            e.run();
        }
        let del_list:Effect[] = [];
        this.effects.forEach(element => {
            let is_run = element.isRun();
            if (!is_run) del_list.push(element);
        });
        del_list.forEach(element => {
            let index = this.effects.indexOf(element);
            if (index != -1) this.effects.splice(index,1);
        });
      }
    
    
      getEffect(effectType:EffectType){
        for(let i=0;i<this.effects.length;i++) {
          let e:Effect = this.effects[i];
          if(e.getName()===effectType){
            return e;
          }
       }
       return null;
      }

}